1 + 1
2
'1' + '1'
'11'
type(1)
int
type(True)
bool
type('1')
str
When you put quotes around text, that text becomes a string (str
).
Strings are sequences of graphical symbols.
2
is the numeric integer "two".
'2'
is the graphical symbol "2".
10 + 7
17
When used with int
s, +
adds them together.
'fire' + 'place'
'fireplace'
When used with str
s, +
concatenates them together.
7 - 3
4
-
does subtraction.
'nickname' - 'name'
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [10], in <cell line: 1>() ----> 1 'nickname' - 'name' TypeError: unsupported operand type(s) for -: 'str' and 'str'
Python doesn't let you subtract strings (only "add" them).
8 == 9
False
9 == 9
True
"cat" == "dog"
False
"cat" == "cat"
True
You use ==
(two equals signs) to see if two things are equal.
You use =
(one equals sign) to give a variable a value.
number = 8
if number == 8:
print('The number is 8')
else:
print('The number is not 8')
The number is 8
8 > 7
True
3 < 8
True
"aardvark" < "zebra"
True
"Bah" > "baccus"
False
'a' > 'Z'
True
"banana" > "apple"
True
Using >
or <
on strings tells you which string comes before the other alphabetically.
pytest
¶road_trip.py
¶break
means break out of the current loop.
It doesn't matter whether the condition is True
or False
, break
will stop the loop.
The next line of code to run is whatever follows the while
loop.
NOTE
Yes:
response == "yes" or response == "Yes"
No:
response == "yes" or "Yes"
int
¶"123" > "23"
False
123 > 23
True
bigger.py
¶input
always returns a string.
You can use int()
to turn a string into an integer.
int
vs str
+
, -
+
also works on strings==
, <
, >
break
or return
to get out of a loop